//============================================================================
// Copyright (C) 2010 Brett R. Jones
// All Rights Reserved
//
// You may redistribute and/or modify for non commercial and commercial uses
// provided this copyright notice remains in place and is not modified
//
// This code is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// brettjones1900@gmail.com
// http://www.p2panarchy.com
//============================================================================

package com.roguetreasure.destroytheworld;

import java.util.List;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class SensorMgr implements SensorEventListener 
{
	private static final String LOG_TAG = "SensorMgr: ";
	private SensorManager sensorManager;
	private List<Sensor> sensors;
	private Sensor m_oOrientationSensor = null;
	private DestroyWorldActivity m_oActivity;
    private static native void nativeOrientationEvent(float f32PosX, float f32PosY, float f32PosZ);

	public SensorMgr(DestroyWorldActivity oActivity) 
	{
		m_oActivity = oActivity;
		this.sensorManager = (SensorManager) m_oActivity.getSystemService(Context.SENSOR_SERVICE);
		if( null != sensorManager)
		{
			this.sensors = sensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
			if (sensors.size() > 0) 
			{
				m_oOrientationSensor = sensors.get(0);
				sensorManager.registerListener(this, m_oOrientationSensor, SensorManager.SENSOR_DELAY_GAME);
			}
		}
	}

	public void onResume() 
	{
		if (m_oOrientationSensor!=null) 
		{
			sensorManager.registerListener(this, m_oOrientationSensor, SensorManager.SENSOR_DELAY_GAME);
		}
	}
	public void onStop () 
	{
		sensorManager.unregisterListener(this);
	}

	public void onAccuracyChanged(Sensor s, int valu) 
	{
	}
	
	public void onSensorChanged(SensorEvent oEvent) 
	{
		if( m_oActivity.m_bShuttingDown)
		{
			return;
		}
		if(oEvent.values.length < 3 )
		{
			return;
		}
		switch(oEvent.sensor.getType())
		{
		case  Sensor.TYPE_ORIENTATION:
			nativeOrientationEvent(oEvent.values[SensorManager.DATA_X],oEvent.values[SensorManager.DATA_Y],oEvent.values[SensorManager.DATA_Z]);
			break;
		}
	}
}
